package com.paipaideli.ui.news;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.paipaideli.R;
import com.paipaideli.common.base.BasePresenterActivity;
import com.paipaideli.common.http.Api;
import com.paipaideli.common.pullrefresh.PullRefreshView;
import com.paipaideli.common.utils.ToastUtil;
import com.paipaideli.common.views.RoundImageView;
import com.paipaideli.ui.news.bean.NewsBean;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by 我的消息
 */
public class MyNewsActivity extends BasePresenterActivity {

    Unbinder unbinder;
    @BindView(R.id.top_back)
    RelativeLayout top_back;
    @BindView(R.id.top_title)
    TextView top_title;
    @BindView(R.id.recycleview_news)
    PullRefreshView recycleview_news;

    private NewAdapter newAdapter;

    private List<NewsBean.Data> list;
    private int curPage = 1;
    private int total;

    @Override
    public int getContentView() {
        return R.layout.activity_news;
    }

    @Override
    public void initView() {
        unbinder = ButterKnife.bind(this);
        top_title.setText("我的消息");
        //返回
        top_back.setOnClickListener(o ->
                finish()
        );

        //获取消息数据
        list = new ArrayList<>();
        recycleview_news.showLoading();
        getData();

        //设置是否在全部加载结束之后Footer跟随内容
        recycleview_news.setOnRefreshListener(refreshlayout -> {
            curPage = 1;
            getData();
            recycleview_news.finishRefresh();
        });

        recycleview_news.setLoadmoreListener(refreshlayout -> {
            if (curPage < total) {
                curPage++;
                getData();
            } else {
                //加载完成 并且 标记为没有更多数据
                refreshlayout.finishLoadmoreWithNoMoreData();
            }
            recycleview_news.finishLoadmore();
        });
        newAdapter = new NewAdapter();
        recycleview_news.setLayoutManager(new LinearLayoutManager(this));
        recycleview_news.setAdapter(newAdapter);

    }

    public void getData() {
        JSONObject requestData = new JSONObject();
        try {
            requestData.put("messageType", "2");
            requestData.put("curPage", curPage);
            requestData.put("pageSize", "10");
        } catch (Exception e) {
            e.getMessage();
        }
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestData.toString());
        Api.getInstanceGson().homeNews(body)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(request -> {
                    if (request.code.equals("200")) {
                        list.addAll(request.data);
                        if (list.size() == 0) {
                            recycleview_news.showEmpty(request.msg);
                            return;
                        }
                        total = request.total;
                        curPage = request.curPage;
                        recycleview_news.finishLoading();
                    } else {
                        recycleview_news.showEmpty(request.msg);
                        ToastUtil.show(request.msg);
                    }
                }, throwable -> {
//                    recycleview_news.showError(throwable);
                });
    }

    public class NewAdapter extends RecyclerView.Adapter {

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(MyNewsActivity.this).inflate(R.layout.item_news, null);
            inflate.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            return new ViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.newTvName.setText(list.get(position).title);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {

            @BindView(R.id.new_image_view)
            RoundImageView newImageView;
            @BindView(R.id.new_tv_name)
            TextView newTvName;
            @BindView(R.id.new_tv_price)
            TextView newTvPrice;
            @BindView(R.id.new_tv_time)
            TextView newTvTime;

            public ViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }
}
